<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * News Controller.
 * 
 * @class     News
 * @author    Iqbal
 * @version   1.0.0
 * @copyright Copyright (c) 2014 IPPI (http://www.ippi.or.id)
 */
class news extends CI_Controller {
    /**
	 * Constructor.
	 */
    function __construct() {
        parent::__construct();
        
        // Load model
        $this->load->model('model_news', 'm_news', TRUE);
    }
    
    /**
	 * Load News Data function.
     * 
     * @param[in] int $offset Offset of Table News data
     * 
     * @return AJAX String
	 */
    function news_data($offset=0){
        // Set variables
        $limit          = get_option('news_limit');
        $condition      = '';
        $this_year      = date('Y');
        
        // Search News
        $search_val     = trim( ippi_isset($this->input->get('search_val'), '') );
        if( !empty($search_val) ) {
            $f          = '(%judul% LIKE "%%s%%" OR %content% LIKE "%%s%%")';
            $condition .= str_replace('%s%', $search_val, $f);
        }
        
        // Filter News
        $filter         = ippi_isset($this->input->get('filter_val'), '');
        if( !empty($filter) ) {
            $filter_by  = $this->input->get('filter_by');
            $and        = ( !empty($search_val) ? ' AND ' : '' );
            
            if( $filter_by == 'archive' ){
                $month_list     = array(
                    'Januari'   => 1, 
                    'Februari'  => 2, 
                    'Maret'     => 3, 
                    'April'     => 4, 
                    'Mei'       => 5, 
                    'Juni'      => 6, 
                    'Juli'      => 7,
                    'Agustus'   => 8, 
                    'September' => 9, 
                    'Oktober'   => 10, 
                    'Nopember'  => 11, 
                    'Desember'  => 12
                );
                $month_num      = sprintf("%02s", $month_list[$filter]);
                $filter         = $this_year . '-' . $month_num;
                $f              = $and . '%datecreated% LIKE "%s%%"';
                $condition     .= str_replace('%s%', $filter, $f);
            }
        }
        
        // Get news data
        $news_data      = $this->m_news->get_all_news_data($limit, $offset, $condition);
        $news           = '';
        $pagination     = '';
        
        // Check response of get news data
        if( !empty($news_data) ){
            $total      = ippi_get_last_found_rows();
            
            foreach($news_data as $row){                
                $news .= '<li>
                    <div class="caption item-inner">
                        <ul class="ul-horizontal">
                            <li>
                                <div class="badge-circle"><i class="glyphicon icon-calendar"></i></div>
                                <time class="time-heading">'. get_date($row->datecreated) . '</time>
                            </li>
                            <li>
                                <a href="'. base_url() . "news/newsdetails/" . $row->id_news.'" class="h4 news-details" data-id="'.$row->id_news.'">
                                    '. ( strlen($row->judul) > 32 ? rtrim(substr($row->judul, 0, 32)) . "..." : $row->judul ). '
                                </a>
                                <dl class="dl-inline">
                                    <dt>Time:</dt>
                                    <dd>' . get_time($row->datecreated) . '</dd>
                                </dl>
                            </li>
                            <li style="display: none;"></li>
                        </ul>
                    </div>
                </li>';
            }
            
            // Set Pagination
            if ($total > $limit){
                $this->load->library('pagination');
                
                $config['base_url']     = 'news/news_data/';
                $config['total_rows']   = $total;
                $config['per_page']     = $limit; 
                $config['anchor_class'] = 'class="pagination_news" ';
                
                $this->pagination->initialize($config);
                $pagination             = $this->pagination->create_links(); 
            }
        }else{
            $news .= '
            <div class="show-more">
                <div class="show-more-inner text-center">
                    <a href="#" class="btn-showmore btn btn-primary btn-sm disabled">No News</a>
                </div>
            </div>';
        }
        
        // Set JSON data
        $data = array(
            'news'              => $news,
            'pagination_news'   => $pagination
        );
        
        // JSON encode data
        die(json_encode($data));
    }
    
    /**
	 * Load News Details function.
     * 
     * @param[in] int $id News ID
     * 
     * @return AJAX String
	 */
    function newsdetails($id=0){
        // Get News Data
        $news   = $this->m_news->get_newsdata($id);
        // Check for response get news
        if( !empty($news) ){
            // Get Event Images
            $news_image         = $this->m_news->get_news_image($id);
            // Set Data View
            $data               = array(
                'news'          => $news,
                'news_id'       => $id,
                'news_image'    => $news_image
            );
            // Set View
            $this->load->view('front/welcome/welcome_news_details',$data);
        }
    }
}

/* End of file news.php */
/* Location: ./application/controllers/news.php */